package com.wq.livepusher.activity;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;

import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.wq.livepusher.R;
import com.wq.livepusher.camera.WlCameraView;
import com.wq.livepusher.push.PushVideo;
import com.wq.livepusher.push.WlBasePushEncoder;
import com.wq.livepusher.push.WlConnectListenr;
import com.wq.livepusher.push.WlPushEncodec;


public class LivePushActivity extends AppCompatActivity {
    private static final String TAG = "LivePushActivity";
    private PushVideo wlPushVideo;
    private WlCameraView wlCameraView;
    private boolean start = false;
    private WlPushEncodec wlPushEncodec;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_livepush);
        wlCameraView = findViewById(R.id.cameraview);
        wlPushVideo = new PushVideo();
        wlPushVideo.setWlConnectListenr(new WlConnectListenr() {
            @Override
            public void onConnecting() {
                Log.d(TAG, "链接服务器中..");
            }

            @Override
            public void onConnectSuccess() {
                Log.d(TAG, "链接服务器成功，可以开始推流了");
                wlPushEncodec = new WlPushEncodec(LivePushActivity.this, wlCameraView.getTextureId());
                wlPushEncodec.initEncodec(wlCameraView.getEglContext(), 720, 1280, 44100, 2);
                wlPushEncodec.startRecord();
                wlPushEncodec.setOnMediaInfoListener(new WlBasePushEncoder.OnMediaInfoListener() {
                    @Override
                    public void onMediaTime(int times) {

                    }

                    @Override
                    public void onSPSPPSInfo(byte[] sps, byte[] pps) {
                        wlPushVideo.pushSPSPPS(sps, pps);
                    }

                    @Override
                    public void onVideoInfo(byte[] data, boolean keyframe) {
                        wlPushVideo.pushVideoData(data, keyframe);
                    }
                });
            }

            @Override
            public void onConnectFail(String msg) {
                Log.d(TAG, msg);
            }
        });
    }

    public void startpush(View view) {

        start = !start;
        if(start)
        {
            wlPushVideo.initLivePush("rtmp://47.106.15.76:1935/myapp/");
        }
        else
        {
            if(wlPushEncodec != null)
            {
                wlPushEncodec.stopRecord();
                wlPushEncodec = null;
            }
        }
    }



}
